<?php
/**
 * 单开公众号升级无线开公众号，指定某一个公众号将之前的单一版数据同步
 */
namespace Plug\multiSync\action;
defined('IN_IA') or exit('Access Denied');
use Plug\Action;
use Plug\multiSync\model\MultiSyncModel;

class MultiSyncPlugAction extends Action
{
    private $MultiSync = null;

    private $_sync_tables = array(
        "kuyuan_red_member",
        "kuyuan_red_media",
        "kuyuan_red_setting",
        "kuyuan_red_red",
        "kuyuan_red_record",
        "kuyuan_red_order",
        "kuyuan_red_refund",
        "kuyuan_red_cash",
        "kuyuan_red_person_publish"
    );

    public function __construct()
    {
        parent::__construct();
        $this->MultiSync = new MultiSyncModel();
    }

    /**
     * 检测系统是否又单开版残余数据
     */
    public function multiSyncTest()
    {
        $_result = false;
        foreach($this->_sync_tables as $_v){
            if(pdo_get($_v,array("uniacid"=>0),array("id"))){
                $_result = true;
                break;
            }
        }
        return $_result;
    }

    /**
     * 获取同步的uniacid
     * @return int
     */
    public function getSyncUniacid()
    {
        if($_sync = $this->MultiSync->findOne(array(),array("uniacid"))){
            return (int) $_sync["uniacid"];
        }else{
            return false;
        }
    }

    /**
     * 获取需要同步的数量
     */
    public function getSyncNumber()
    {
        $_total = 0;
        foreach($this->_sync_tables as $_v){
            $_total += $this->MultiSync->getSyncNumber($_v);
        }
        return $_total;
    }

    /**
     * 获取同步的数据表
     */
    public function getSyncTables()
    {
        if(!$this->MultiSync->findOne()){
            $this->MultiSync->insert(array("uniacid"=>$this->_uniacid));
        }
        $_folder = ATTACHMENT_ROOT.'kuyuan_red/0';
        if(is_dir($_folder)){
            rename($_folder,ATTACHMENT_ROOT.'kuyuan_red/'.$this->_uniacid);
        }
        return $this->_sync_tables;
    }
}
